const fs = require('fs')
const path = require('path')
const {
  json
} = require('stream/consumers')
fs.readFile(path.join(__dirname, 'data', 'data.json'), (err, data) => {
  if (err) console.log(err)
  // console.log(data.toString())
  const list = JSON.parse(data.toString())
  //创建两个空数组接收
  const boyList = []
  const girlLIst = []
  //遍历每个姓名对象
  list.forEach(item => {
    if (item.gender === '男') boyList.push(item)
    else if (item.gender === '女') girlLIst.push(item)
  })
  // console.log(boyList,girlLIst)

  //插入到目标文件
  fs.writeFile(path.join(__dirname, 'data/男.json'), JSON.stringify(boyList), err => {
    if (err) console.log(err)
    console.log('男性写入成功')
  })
  fs.writeFile(path.join(__dirname, 'data/女.json'), JSON.stringify(girlLIst), err => {
    if (err) console.log(err)
    console.log('男性写入成功')
  })
})